<?php
namespace Admin\Controller;

use Util\AccessController;

/*
 * 仓库
 */

class StoreHouseController extends AccessController {
    public function index() {
        $LS = Service('StoreHouse', 'ck');
        $DATA = $LS->query();
        $this->DATA = $DATA;
        $this->display();
    }

    public function SJindex() {
        $LS = Service('StoreHouse', 'sj');
        $where['CK_ID'] = $_GET['BHCODE'];
        $DATA = $LS->querySJ($where, 'ts_sj.*,ts_sj.CODE as SJCODE,ts_sj_model.TITLE,ts_sj_model.SG_NUM');
        $this->BHCODE = $_GET['BHCODE'];
        $this->DATA = $DATA;

        $LS = Service('BookCaseModel', 'sj_model');
        $MDATA = $LS->query();
        $this->MDATA = $MDATA;
        $this->display();
    }

    public function SGindex() {
        $LS = Service('StoreHouse', 'sg');
        $where['SJ_ID'] = $_GET['BHCODE'];
        $DATA = $LS->query($where);
        $this->BHCODE = $_GET['BHCODE'];
        $this->DATA = $DATA;

        $this->MRWZID = $this->S('MRWZID');
        $this->display();
    }

    public function Bookindex() {
        $LS = Service('StoreHouse', 'wz');
        $where['ts_wz.WZ_ID'] = $_GET['BHCODE'];
        $files = C('ADMIN_BOOK_LS') . ',ts_book_gg.GG_NAME,ts_wz.ID as WZ_ID,ts_wz.GG_ID as WZ_GG_ID,ts_wz.USER_ID';
        $DATA = $LS->GETBOOK($where, null, $files, 'left join ts_basket on ts_book.ID = ts_basket.BOOK_ID');
        //        dump($DATA);
        //        $ALLNUM = $LS->query($where, null, 'sum(STOCK) as ALLNUM ');
        $this->BHCODE = $_GET['BHCODE'] ? $_GET['BHCODE'] : 0;
        $this->NUM = count($DATA);
        //        $this->ALLNUM = $ALLNUM[0]['ALLNUM'];

        //仓库位置
        $LS2 = Service('Book', 'ck');
        $ckdata = $LS2->query();
        $this->CKDATA = $ckdata;

        //准确位置
        $LS3 = Service('Book','sg');
        $WZDATA = $LS3->GetWZ1($_GET['BHCODE']);
        $this->WZDATA = $WZDATA;

        $this->DATA = $DATA;
        $this->display();
    }

    /*
     * 移除书籍在书柜上的位置
     */
    public function BookClearSG() {
        $LS = Service('StoreHouse', 'book');
        $where['ID'] = $_GET['ID'];
        $save['WZ_CODE'] = '';
        $save['OLD_WX_CODE'] = $_GET['WX_CODE'];
        $save['STATE'] = '未入库';
        $OK = $LS->update($where, $save);
        $OK ? $this->success('移除成功') : $this->error('移除失败');
    }

    public function AddIndex() {
        $LS = Service('StoreHouse', 'ck');
        $data = $LS->find(array('ID' => $_GET['ID']));
        $this->DATA = $data;
        $this->display();
    }

    public function AddHanld() {
        $save = $_POST;
        $save['BHCODE'] = 'CK' . uniqid() . rand(0, 9);
        $LS = Service('StoreHouse', 'ck');
        if ($_POST['ID']) {
            $where['ID'] = $_POST['ID'];
            $update = $_POST;
            $OK = $LS->update($where, $update);
        } else {
            $OK = $LS->insert($save);
        }
        $OK ? $this->success('添加/编辑成功') : $this->error('添加/编辑失败');
    }

    public function AddSJ() {
        $DATA = $_POST;
        $DATA['BHCODE'] = 'SJ_' . $_POST['CK_ID'] . '_' . uniqid();
        $LS = Service('StoreHouse', 'sj');
        $OK = $LS->AddSJ($DATA);
        $OK ? $this->success('添加成功') : $this->error('添加失败');
    }

    public function DelSJ() {
        $LS = Service('StoreHouse', 'sj');
        $OK = $LS->DelSJ($_GET['ID']);
        $OK ? $this->success('移除成功') : $this->error('移除失败');

    }

    public function DelCK() {
        $LS = Service('StoreHouse', 'ck');
        $OK = $LS->DelCK($_POST['ID']);
        if ($OK) {
            echo 1;
        } else {
            echo 0;
        }

    }

    public function UpdateSGNAME() {
        $where['ID'] = $_POST['ID'];
        $save['NAME'] = $_POST['NAME'];
        $LS = Service('StoreHouse', 'sg');
        $OK = $LS->update($where, $save);
        $OK ? $this->success('修改成功') : $this->error('修改失败');
    }

    /*
     * 录入库存
     * 添加位置
     */
    public function Select() {
        if ($_POST['name'] == 'sj') {
            $LS = Service('StoreHouse', 'sj');
            $DATA = $LS->query(array('CK_ID' => $_POST['ID']));
            $data['name'] = 'sg';

        }
        if ($_POST['name'] == 'sg') {
            $LS = Service('StoreHouse', 'sg');
            $DATA = $LS->query(array('SJ_ID' => $_POST['ID']));
            $data['name'] = 'wz';
        }

        if ($_POST['name'] == 'wz') {
            //移动
            if($_POST['YDWZS']){
                $successNum = 0;
                foreach($_POST['YDWZS'] as $k =>$v){
                    $LS = Service('M','wz');
                    $where['ID'] = $v;
                    $save['WZ_ID'] = $_POST['ID'];
                    $OK = $LS->update($where,$save);
                    $OK?$successNum++:1;
                }
                $DATA = array();
                $data['BHCODE'] = $_POST['ID'];
                $data['OK'] = $successNum;
            }else {
                $LS = Service('StoreHouse', 'wz');
                $OK = $LS->addwz($_POST);
                $DATA = array();
                $data['name'] = '1';
                $data['OK'] = $OK;
            }
        }

        $data['datas'] = $DATA;

        $this->ajaxReturn($data, 'JSON');
    }

    /*
     * 设置默认书柜
     */
    public function SetMrSg() {
        $this->S('MRWZID', $_GET['BHCODE']);
        $this->success('设置成功');
    }

    /*
     * 移动到收书数据库
     */
    public function YDSBOOKCK(){
        $LS = Service('M','book');
        $LS1 = Service('M','sbook');
        $ALL =array();
        foreach($_POST['BOOKIDS'] as $k =>$v){
            $where['ID'] = $v;
            $data = $LS->find($where);
            $HOK = $LS1->find(array('TITLE'=>$data['TITLE']));
            if(!$HOK){
                unset($data['STATE']);
                unset($data['ID']);
                $ALL[] =$data;
            }

        }
//        echo json_encode($ALL);
        $LS = Service('M','sbook');
        $OK = $LS->AddAll($ALL);
        echo $OK;
    }

    //由isbn获取书籍
    public function GetBookToISBN(){
        $LS =Service('M','book_gg_view');
        $where['ISBN'] = $_POST['ISBN'];
        $order = 'GG_NAME asc';
        $data = $LS->query($where,null,null,$order);
        $this->ajaxReturn($data,'JSON');

    }

    //书籍添加位置
    public function BookAddWZ(){
        $LS = Service('Book', 'wz');
        $GG_ID = $_POST['GG_ID'];
        $WZ_CODE = $_POST['WZ_CODE'];
        $OK = $LS->AddWz($GG_ID, $WZ_CODE);
        echo $OK;
    }
}